
#include <gtest/gtest.h>

#include "buffer.h"

#include <string>

class TestBuffer: public ::testing::Test
{
public:
  virtual void SetUp()
  {
  }

  virtual void TearDown()
  {

  }

};

TEST_F(TestBuffer, test_normal_op)
{
  buffer buf;
  ASSERT_EQ(buf.capacity(), BUFSIZ);
  ASSERT_EQ(buf.assign("123").compare("123"), 0);
  ASSERT_EQ(buf.assign("A").compare("A"), 0);
  ASSERT_EQ(buf.assign("##########").compare("##########"), 0);
  ASSERT_EQ(buf.assign("bar foo").compare("bar foo"), 0);
  ASSERT_EQ(buf.append("2").compare("bar foo2"), 0);
  ASSERT_EQ(buf.append("3456", 2).compare("bar foo234"), 0);
  ASSERT_EQ(buf.append(56).compare("bar foo23456"), 0);
  ASSERT_EQ(buf.append(true).compare(buffer().assign("bar foo23456true")), 0);
  ASSERT_EQ(buf.assign(0).compare("0123", 1), 0);

  buffer::iterator iter = buf.begin();
  ASSERT_EQ(*iter, '0');
  ASSERT_EQ(++iter == buf.end(), true);
  buf.append(buffer().assign("123456789"));
  ASSERT_EQ(std::string(buf.ptr(), buf.length()), std::string("0123456789"));
  ASSERT_NE(iter++ == buf.end(), true);
  ASSERT_EQ(*iter, '2');
  ASSERT_EQ(buf.pos(iter), 2);
  
  buffer::const_iterator iterc = buf.begin();
  for(; iterc != buf.end(); iterc++)
  {
    ASSERT_EQ(*iterc, buf.ptr()[buf.pos(iterc)]);
  }
}

